﻿package  {
	import flash.display.MovieClip;
	import flash.events.Event;
	import flash.sampler.Sample;
	import com.greensock.TweenMax;
	
	import Wheel;
	import Logic;
	
	public class Reels extends MovieClip {
		
		//params
		public var linesCount:int = 9;
		//layers
		public var reelBack:MovieClip;
		
		public var reelWheel1:Wheel;
		public var reelWheel2:Wheel;
		public var reelWheel3:Wheel;
		public var reelWheel4:Wheel;
		public var reelWheel5:Wheel;
		
		public var reelAnims:MovieClip;
		public var reelBoxes:MovieClip;
		public var reelLines:MovieClip;
		public var reelFront:MovieClip;
		
		//contents
		public var animsArray:Array;
		public var boxesArray:Array;
		public var linesArray:Array;
		
		
		
		
		
		//other
		public var playLines:int = 9;
		public var playNumbers:Array = [1,1,1,1,1,1,1,1,1,1,1,1,1,1,1];
		
		public var playWins:Array;
		
		
		public var wnow:int = 0;
		
		
		
		
		//temps
		public var tmp1,tmp2,tmp3,tmp4;
		
		
		
		
		public function Reels() {
			super();
			// constructor code
			
			this.reelBack = new MovieClip();
			this.reelWheel1 = new Wheel();
			this.reelWheel2 = new Wheel();
			this.reelWheel3 = new Wheel();
			this.reelWheel4 = new Wheel();
			this.reelWheel5 = new Wheel();
			this.reelAnims = new MovieClip();
			this.reelBoxes = new MovieClip();
			this.reelLines = new MovieClip();
			this.reelFront = new MovieClip();
			
			//construct;
			this.addChild(this.reelBack);
			this.addChild(this.reelWheel1);
			this.addChild(this.reelWheel2);
			this.addChild(this.reelWheel3);
			this.addChild(this.reelWheel4);
			this.addChild(this.reelWheel5);
			this.addChild(this.reelAnims);
			this.addChild(this.reelBoxes);
			this.addChild(this.reelLines);
			this.addChild(this.reelFront);
			
			
			
			this.addEventListener(Event.ADDED_TO_STAGE, this.onStage);
		}
		

		
		
		
		
		public function onStage(e:Event):void
		{
			this.removeEventListener(Event.ADDED_TO_STAGE, this.onStage);
			
			this.build();

		}
		
		public function build():void
		{
			//fill back
			var i:int;
			//var back1:MovieClip = Helper.createClass('reel_back');
			//this.reelBack.addChild(back1);
			
			this.reelWheel1.x = 0*131;
			this.reelWheel2.x = 1*131;
			this.reelWheel3.x = 2*131;
			this.reelWheel4.x = 3*131;
			this.reelWheel5.x = 4*131;
			
			this.reelWheel1.resetWheel(1,2,3);
			this.reelWheel2.resetWheel(1,10,3);
			this.reelWheel3.resetWheel(1,2,3);
			this.reelWheel4.resetWheel(1,2,3);
			this.reelWheel5.resetWheel(1,2,3);
			
			//animsArrayFill
			this.animsArray = new Array();
			var va:WAnim;
			for(i=1; i<=15; i++)
			{
				va = new WAnim();
				va.addFromLib('anim_bonus');
				va.addFromLib('anim_scatter');
				va.addFromLib('anim_wild');
				this.animsArray.push(va);
				va.y = ((i-1) % 3)*128;
				va.x = (Math.floor((i-1) / 3))*131.5;
				this.reelAnims.addChild(va);
			}
			
			//boxesArray Fill
			this.boxesArray = new Array();
			for(i=1; i<=15; i++)
			{
				va = new WAnim();
				va.addFromLib('box_main');
				this.boxesArray.push(va);
				va.y = ((i-1) % 3)*128;
				va.x = (Math.floor((i-1) / 3))*131.5;
				this.reelBoxes.addChild(va);
			}
			
			//linesArray Fill
			this.linesArray = new Array();
			var ln:MovieClip;
			for(i=1; i<=Logic.reelLines; i++)
			{
				ln = Helper.createClass('line_'+i);
				ln.gotoAndStop(1);
				this.linesArray.push(ln);
				this.reelLines.addChild(ln);
			}
			
			//add something infront of reels
			
		} //build() end;
		
		
		
		//PUBLIC
		
		public function spinAll():void
		{
			//dev: create random;
			this.playNumbers = new Array();
			for(var i = 0; i< 15;i++)
			{
				this.playNumbers.push(Helper.random(1,13));
			}
			
			
			
			
			var ref = this;
			
			this.wnow = 5;
			var f = function(){
				ref.wnow --;
				if(ref.wnow == 0)
				{
					ref.weelsDone();
				}
			};
			this.reelWheel1.onStopWheel = f;
			this.reelWheel2.onStopWheel = f;
			this.reelWheel3.onStopWheel = f;
			this.reelWheel4.onStopWheel = f;
			this.reelWheel5.onStopWheel = f;
			
			
			
			this.showSelectedLines();
			
			TweenMax.delayedCall(.1, function(){
				ref.reelWheel1.startWheelTo(ref.playNumbers[0],ref.playNumbers[1],ref.playNumbers[2]);
			});
			TweenMax.delayedCall(.2, function(){
				ref.reelWheel2.startWheelTo(ref.playNumbers[3],ref.playNumbers[4],ref.playNumbers[5]);
			});
			TweenMax.delayedCall(.3, function(){
				ref.reelWheel3.startWheelTo(ref.playNumbers[6],ref.playNumbers[7],ref.playNumbers[8]);
			});
			TweenMax.delayedCall(.4, function(){
				ref.reelWheel4.startWheelTo(ref.playNumbers[9],ref.playNumbers[10],ref.playNumbers[11]);
			});
			TweenMax.delayedCall(.5, function(){
				ref.reelWheel5.startWheelTo(ref.playNumbers[12],ref.playNumbers[13],ref.playNumbers[14]);
			});
		}
		
		public function weelsDone():void
		{
			trace("done");
			this.showWinLines();
		}
		
		public function showSelectedLines(full:Boolean = false):void
		{
			var i;
			var ps:int = full ? 3 : 2;
			for(i=0;i<Logic.reelLines;i++)
			{
				ps = i<this.playLines ? ps : 1;
				this.linesArray[i].gotoAndStop(ps);
			}
		}
		
		public function showWinLines():void
		{
			//calculate win lines
			var winned_lines:Array = new Array(); //array[LineNo,color,amount,symbs_array]
			var save_arr:Array;
			var i,j;
			var counts, line_symbol, total_win;
			var line_info:Array;
			for(i=0;i<this.playLines;i++)
			{
				line_info = Logic.winLines[i];
				//front calc
				counts = 0;
				line_symbol = 0;
				total_win = 0;
				for(j=0;j<line_info.length;j++)
				{
					if(line_symbol == 0) //just started
					{
						line_symbol = this.playNumbers[line_info[j]];
						counts++;
						continue;
					}
					if(this.playNumbers[line_info[j]] == Logic.wildNumber)
					{
						counts++;
						continue;
					}
					if(line_symbol == Logic.wildNumber) //line starts from wild but now not wild
					{
						line_symbol = this.playNumbers[line_info[j]];
						counts++;
						continue;
					}
					if(line_symbol == this.playNumbers[line_info[j]])
					{
						counts++;
						continue;
					}
					break;
				}
				//calulate by symbols;
				total_win = Logic.winSymbols[line_symbol+1][counts];
				if(total_win > 0)
				{
					save_arr = new Array();
					save_arr.push(i+1); //first line !1!
					//save to [Line,Symbols]
					for(j=0;j<counts;j++)
					{
						save_arr.push(line_info[j]);
					}
					winned_lines.push(save_arr);
				}
			}
			this.playWins = winned_lines;
			//We gathered lines:
			
			//TODO: START WILDS IF THEY ON LINES
			trace(this.playWins);
			
			//START ANIMATION SEQUENCE
			if(this.playWins.length > 0)
			{
				this.startWins();
			}
			
			
			
			
			
			
			
		}
		
		public function startWins():void
		{
			trace("startWins");
			this.tmp1 = 0;
			var ref = this;
			var i,j;
			//blink 3 times;
			for(i=0;i<this.playWins.length;i++)
			{
				trace(this.playWins[i][0]);
				this.linesArray[this.playWins[i][0]-1].gotoAndStop(3);
			}
			
			
			
		}
		
		
		public function stopWins():void
		{
			
		}
		
		
		
		
	}
	
}
